package com.example.musicaiap.ui.main

import android.util.Log // Import Log
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject
import kotlin.random.Random

@HiltViewModel
open class MainViewModel @Inject constructor() : ViewModel() {

    // Private mutable state flow for the global background color
    // Initialize with a default color (e.g., transparent or initial theme background)
    // Using Transparent might be better initially until a random color is set.
    private val _globalBackgroundColor = MutableStateFlow(Color.Transparent)

    // Public immutable state flow exposed to the UI
    val globalBackgroundColor: StateFlow<Color> = _globalBackgroundColor.asStateFlow()

    /**
     * Generates a new random color and updates the global background color state.
     */
    fun updateGlobalBackgroundColor() {
        Log.d("MainViewModel", "updateGlobalBackgroundColor called") // Add log
        val randomColor = Color(
            red = Random.nextInt(256),
            green = Random.nextInt(256),
            blue = Random.nextInt(256)
        )
        Log.d("MainViewModel", "Generated random color: $randomColor") // Add log
        _globalBackgroundColor.value = randomColor
    }

    /**
     * Resets the global background color to transparent, allowing the theme default to show.
     * (Optional: Could be useful for a reset button later)
     */
    fun resetGlobalBackgroundColor() {
        _globalBackgroundColor.value = Color.Transparent
    }
}
